# Overwrite alpha function
alpha <- psych::alpha

# Load data
data <- readRDS("Data/panel/panel_data.rds")

# Identify participants who appear more than three times
panelists <- data %>%
    group_by(id) %>%
    count() %>%
    subset(n >= 3) %>%
    pull(id)

# Subset to panelists
data <- data %>%
    subset(id %in% panelists)

# Calculate alphas
## Norm perceptions
data %>%
    select(starts_with("norm_") & ends_with("_perception")) %>%
    alpha()

## Violence perceptions
data %>%
    select(starts_with("violence_") & ends_with("_perception")) %>%
    alpha()

## Norms
data %>%
    select(starts_with("norm_") & !matches("_perception$|_index$")) %>%
    alpha()

## Violence
data %>%
    select(starts_with("violence") & !matches("_perception$|_index$")) %>%
    alpha()

# Generate indexes
## Norm perceptions
data$norm_per_index <- data %>%
    select(starts_with("norm_") & ends_with("_perception")) %>%
    rowMeans(na.rm = TRUE)

## Violence perceptions
data$violence_per_index <- data %>%
    select(starts_with("violence_") & ends_with("_perception")) %>%
    rowMeans(na.rm = TRUE)

## Norms
data$norm_index <- data %>%
    select(starts_with("norm") & !matches("_perception$|_index$")) %>%
    rowMeans(na.rm = TRUE)

## Violence
data$violence_index <- data %>%
    select(starts_with("violence") & !matches("_perception$|_index$")) %>%
    rowMeans(na.rm = TRUE)

## Affective polarization
data$aff_pol <- data$in_party_therm - data$out_party_therm

# Rename variables
data <- data %>%
    rename(
        Panelist = id,
        `Survey Date` = survey_time
    )

# Two-way fixed effects models
norm_model <- feols(
    norm_index ~ norm_per_index + violence_per_index | Panelist + `Survey Date`,
    data = data
)

violence_model <- feols(
    violence_index ~ norm_per_index + violence_per_index | Panelist + `Survey Date`,
    data = data
)

aff_pol_model <- feols(
    aff_pol ~ norm_per_index + violence_per_index | Panelist + `Survey Date`,
    data = data
)

# Regression table
modelsummary(
    list(
        "Partisan Animosity (Affective Polarization)" = aff_pol_model,
        "Support for Partisan Violence" = violence_model,
        "Support for Undemocratic Practices" = norm_model
    ),
    coef_map = c(
        "violence_per_index" = "Perceived Out-Party Support for Partisan Violence",
        "norm_per_index" = "Perceived Out-Party Support for Undemocratic Practices"
    ),
    stars = TRUE,
    title = '"Effect" of Out-Party Perceptions Moving One Percentage Point on Hostile Attitudes',
    notes = "Note: Independent variables are average indexes of panelists' out-party perceptions. Dependent variables are as follows: the difference between panelists' in-party warmth and out-party warmth (affective polarization), an average index capturing panelists' support for partisan violence, and an average index capturing panelists' support for undemocratic practices. Models include panelist and survey-date fixed effects. Standard errors clustered by panelist.",
    output = "Tables/panel_models.txt"
)
